<template>
  <Login v-if="!token"/>
  <Question v-else />
</template>

<script>
import Login from './Login.vue'
import Question from './question.vue'

export default {
  name: 'Index',
  data() {
    return {
      token: ''
    }
  },
  components: {
    Login,
    Question,
  },
  beforeUnmount() {
    window.removeEventListener('setItemEvent', this.handleLocalStorageChange);
  },
  mounted() {
    this.token = window.localStorage.getItem("token")
    window.addEventListener("setItemEvent", this.handleLocalStorageChange)
  },
  methods: {
    handleLocalStorageChange(event) {
      if (event.key === "token") {
        this.token = event.newValue;
      }
    },
  }
}
</script>